import { defineStore } from 'pinia'
import { ref } from 'vue'
import {
  passwordLogin as _passwordLogin,
  smsLogin as _smsLogin,
  getSmsCode as _getSmsCode,
  register as _register,
  logout as _logout
} from '@/api/login'
import { toast } from '@/utils/toast'
import { IUserInfoVo } from '@/api/types/login'

// 初始化状态
const userInfoState: IUserInfoVo = {
  id: 0,
  // username: '',
  avatar: '/static/images/default-avatar.png',
  token: '',
}

export const useUserStore = defineStore(
  'user',
  () => {
    // 定义用户信息
    const userInfo = ref<IUserInfoVo>({ ...userInfoState })
    // 设置用户信息
    const setUserInfo = (val: IUserInfoVo) => {
      console.log('设置用户信息', val)
      // 若头像为空 则使用默认头像
      if (!val.avatar) {
        val.avatar = userInfoState.avatar
      }
      userInfo.value = val
      uni.setStorageSync('userInfo', val)
      uni.setStorageSync('token', val.token)
    }
    // 删除用户信息
    const removeUserInfo = () => {
      userInfo.value = { ...userInfoState }
      uni.removeStorageSync('userInfo')
      uni.removeStorageSync('token')
    }
    /**
     * 密码登录
     */
    const passwordLogin = async (credentials: { mobile: string, password: string }) => {
      const res = await _passwordLogin(credentials)
      console.log('登录信息', res)
      const tempUserInfo: IUserInfoVo = {
        id: Number(res.data.id),
        // username: res.data.username,
        avatar: '',
        token: res.data.token
      }
      setUserInfo(tempUserInfo)
      toast.success('登录成功')

      return res
    }

    /**
     * 验证码登录
     */
    const smsLogin = async (credentials: { mobile: string, code: string }) => {
        const res = await _smsLogin(credentials)
        console.log('登录信息', res)

        const tempUserInfo: IUserInfoVo = {
          id: Number(res.data.id),
          avatar: '',
          token: res.data.token
        }
        setUserInfo(tempUserInfo)
        toast.success('登录成功')
        return res
     
    }

    /**
     * 获取验证码
     */
    // ... existing code ...
    const getSmsCode = async (data: { mobile: string, type: string }) => {
      // 确保mobile和type参数存在且有效
      if (!data.mobile || !data.type) {
        throw new Error('手机号和验证码类型不能为空')
      }

      try {
        const res = await _getSmsCode(data)
        // 根据实际API响应结构调整
        console.log(`发送到 ${data.mobile} 的验证码请求成功`,res.data.code)

        toast.success('验证码发送成功')
        return res
      } catch (error) {
        console.error('发送验证码失败:', error)
        toast.error('发送验证码失败，请重试')
        throw error
      }
    }
// ... existing code ...

    /**
     * 用户注册
     */
    const register = async (data: { mobile: string, code: string, password: string }) => {
      const res = await _register(data)
      toast.success('注册成功')
      return res
    }

    /**
     * 退出登录 并 删除用户信息
     */
    const logout = async () => {
      await _logout()
      removeUserInfo()
      toast.success('退出成功')
    }

    return {
      userInfo,
      setUserInfo,
      removeUserInfo,
      passwordLogin,
      smsLogin,
      getSmsCode,
      register,
      logout
    }
  }
)